package ca.uwindsor.cs.deepweb.utility.sougouweb;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;

import org.xml.sax.InputSource;

public class SogouWebCorpusDocumentHandler {

	protected String content;

	protected String url;

	protected String docid;

	private BufferedReader bufferedreader_in;

	private String string_line;

	private StringBuffer temp;

	/**
	 * Constructor
	 * 
	 * @param contentstring
	 */
	public SogouWebCorpusDocumentHandler() {
	}

	/**
	 * Process Document and store URL, DOCID, and Content
	 * 
	 * @param is
	 *            Input Source, it can come from any source, e.g. String or
	 *            File. An example that convert string to input source:
	 *            ByteArrayInputStream str = new
	 *            ByteArrayInputStream(buf.toString().getBytes("UTF-8"));
	 *            handler.processDocument(new InputSource(str));
	 * @throws Exception
	 *             Any exception that cause by parsing document
	 * 
	 */
	public void processDocument(InputSource is) throws Exception {

		bufferedreader_in = new BufferedReader(is.getCharacterStream());

		string_line = bufferedreader_in.readLine();
		temp = new StringBuffer();
		while (string_line != null) {
			if (string_line.contains("<url>")) {
				url = string_line.replace("<url>", "").replace("</url>", "");
				string_line = bufferedreader_in.readLine();
			} else if (string_line.contains("<docid>")) {
				docid = string_line.replace("<docid>", "").replace("</docid>",
						"");
				string_line = bufferedreader_in.readLine();
			} else {
				temp.append(string_line);
				temp.append(" ");
				string_line = bufferedreader_in.readLine();
			}
		}
		bufferedreader_in.close();
		int startindex = temp.indexOf("<content>");		
		temp.delete(0, startindex);
		int endindex = temp.indexOf("</content>");
		temp.delete(endindex, temp.length()-1);
		content=temp.toString().replace("<content>", "");
//		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
//				.newInstance();
//		DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
//		Document doc = docBuilder.parse(is);
//		doc.getDocumentElement().normalize();
//		// root node
//		Node node = doc.getFirstChild();
//		// all sub nodes
//		NodeList list = node.getChildNodes();
//		for (int i = 0; i < list.getLength(); i++) {
//			Node nodeitm = list.item(i);
//			if (nodeitm instanceof Element) {
//				if (nodeitm.getNodeName().equals("url")) {
//					url = nodeitm.getFirstChild().getNodeValue().trim();
//				} else if (nodeitm.getNodeName().equals("docid")) {
//					docid = nodeitm.getFirstChild().getNodeValue().trim();
//				} else if (nodeitm.getNodeName().equals("content")) {
//					content = nodeitm.getFirstChild().getNodeValue().trim();
//				}
//			}
//		}
	}

	/**
	 * The content of Sogou Web document
	 * 
	 * @return content
	 */
	public String getContent() {
		return content;
	}

	/**
	 * The document ID of Sogou Web document, e.g.
	 * 099c0797fcbfce89-ddf13306c0bb3300
	 * 
	 * @return DocID
	 */
	public String getDocID() {
		return docid;
	}

	/**
	 * The URL of Sogou Web document
	 * 
	 * @return URL
	 */
	public String getURL() {
		return url;
	}

	/**
	 * For testing only, and give an example
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// for testing only
		StringBuffer buf = new StringBuffer();
		buf.append("<doc>");
		buf.append("<url>http://www.blogworld.com.cn/</url>");
		buf.append("<docid>0201dbe57c7c04a8-24c0cc9a44313b00</docid>");
		buf
				.append("<content>ｂｌｏｇ皮肤，博客模板，博客皮肤部落窝是我们大家共同的家园，在这里分享我们生活的点滴收获和快乐，所有悲伤，失落，让我们一起走过……想过在自己的Ｂｌｏｇ上卖东西吗？有过偶尔有东西想要出售但又不愿去为此开店的烦恼吗？想过利用自己Ｂｌｏｇ的人气，一边写文章一边赚点小钱吗？部落窝Ｂｌｏｇ麦场让您轻松开麦，让我们通过Ｂｌｏｇ来交易，交易同时来交朋友！部落窝居为广大居民提供一个极具个性的Ｆｌａｓｈ音乐盒＂ＷｏＢｏｘ＂。＂ＷｏＢｏｘ＂既支持Ｍｐ３，也支持ｗｍａ，ｗｍｖ格式，还提供一个形象Ｓｈｏｗ的窗口，可以上传自己的个性图片。广大居民既可以选择部落窝提供的歌曲，也可以任意加入自己喜欢的歌曲链接地址，轻轻松松让你的Ｂｌｏｇ变成一个属于自己的音乐世界。</content>");
		buf.append("</doc>");
		SogouWebCorpusDocumentHandler handler = new SogouWebCorpusDocumentHandler();
		try {
			ByteArrayInputStream str = new ByteArrayInputStream(buf.toString()
					.getBytes("UTF-8"));
			handler.processDocument(new InputSource(str));
			System.out.println(handler.getContent());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(-1);
		}
	}

}
